package com.springboot.controller;

import com.springboot.dto.DoctorMessageList;
import com.springboot.model.AssistantMessageInform;
import com.springboot.model.MessageInform;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewMessageListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 本控制台用于医生查看通知列表
 */
@Controller
public class DoctorViewMessageListController {
    @Autowired
    DoctorViewMessageListService doctorViewMessageListService;
    /**
     * 医生查看通知列表
     */
    @CrossOrigin
    @RequestMapping(value = "/getInfoServlet")
    public @ResponseBody
    String doctorViewMessageList(@RequestParam("username") String doctorTel) {
        System.out.println("医生查看通知列表");
        try {
            UserLoginForm doctor=doctorViewMessageListService.findUserByTel(doctorTel);
            int doctorId=doctor.getUserId();
            //只显示三天内的已读通知和所有的未读通知
            ArrayList<MessageInform> messageInforms=doctorViewMessageListService.findMessageListByDoctorId(doctorId);
            System.out.println(messageInforms.size());
            ArrayList<DoctorMessageList> doctorMessageLists=new ArrayList<>();
            if(messageInforms.size()==0)
            {
                return "空";
            }
            else
            {
                for(int i=0;i<messageInforms.size();i++)
                {
                    MessageInform messageInform=messageInforms.get(i);
                    DoctorMessageList doctorMessageList=new DoctorMessageList();
                    doctorMessageList.setInfoId(messageInform.getMessageId());
                    doctorMessageList.setInfoType(messageInform.getMessageMark());
                    doctorMessageList.setState(messageInform.getMessageState2());
                    SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String current=dateTime.format(messageInform.getMessageDatetime());
                    String []currArray = current.split("\\s+");
                    doctorMessageList.setCurrDate(currArray[0]);
                    doctorMessageList.setCurrTime(currArray[1]);
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                    doctorMessageList.setAppointDate(format.format(messageInform.getBookDate()));
                    doctorMessageList.setAppointTime(messageInform.getBookTime());
                    int patientId=messageInform.getPatientId();
                    UserLoginForm userLoginForm=doctorViewMessageListService.findUserById(patientId);
                    doctorMessageList.setPatientName(userLoginForm.getUserName());
                    doctorMessageList.setPatientTel(userLoginForm.getUserTel());
                    doctorMessageList.setDoctorName(doctor.getUserName());
                    doctorMessageList.setDoctorTel(doctorTel);
                    if(messageInform.getMessageMark().equals(4))
                    {
                        AssistantMessageInform assistantMessageInform=new AssistantMessageInform();
                        assistantMessageInform.setMessageId(messageInform.getMessageId());
                        assistantMessageInform.setMessageState4(1);
                        AssistantMessageInform assistantMessage=doctorViewMessageListService.findAssistantByMessage(assistantMessageInform);
                        int assistantId=assistantMessage.getAssistantId();
                        UserLoginForm assistant=doctorViewMessageListService.findUserById(assistantId);
                        doctorMessageList.setAssistantName(assistant.getUserName());
                        doctorMessageList.setAssistantTel(assistant.getUserTel());
                    }
                    doctorMessageLists.add(doctorMessageList);
                }
            }
            //按消息日期和消息时间排序
            Comparator<DoctorMessageList> comparator = new Comparator<DoctorMessageList>(){
                public int compare(DoctorMessageList s1, DoctorMessageList s2) {
                    //先按照消息日期排序
                    if(!s1.currDate.equals(s2.currDate)){
                        return s2.currDate.compareTo(s1.currDate);
                    }
                    else{
                        //消息日期相同则按照消息时间排序
                        return s2.currTime.compareTo(s1.currTime);
                    }
                }
            };
            //这里就会自动根据规则进行排序
            Collections.sort(doctorMessageLists,comparator);
            System.out.println(doctorMessageLists.toString());
            return doctorMessageLists.toString();
        } catch (Exception e) {
            return "False";
        }
    }
}